/* 
 * Copyright 2011 VelocitiSoftware
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.velociti.ikarus.ui.widget;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.StringTokenizer;

import com.vaadin.data.Container;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.terminal.PaintException;
import com.vaadin.terminal.PaintTarget;
import com.vaadin.ui.ClientWidget;
import com.velociti.ikarus.widget.client.ui.VIkarusOptionGroup;

/** #### IkarusOptionGroup ####
 * By setting alignment property component will align its options,
 * HORIZONTALLy or VERTICALLY. Default alignment is HORIZONTAL. 
 * If there is not enough width to layout HORIZONTALLy component
 * will layout its options VERTICALLY.
 * 
 * @author Alper Turkyilmaz - VelocitiSoftware - 2011
 * @version 1.0.1
 */

@ClientWidget(VIkarusOptionGroup.class)
public class IkarusOptionGroup extends com.vaadin.ui.OptionGroup {
	public static final String HORIZONTAL = "horizontal";
	public static final String VERTICAL = "vertical";

	private String alignment;

	public IkarusOptionGroup() {
		super();
		alignment = HORIZONTAL;
	}

	public IkarusOptionGroup(String caption, Collection<?> options) {
		super(caption, options);
		alignment = HORIZONTAL;
	}

	public IkarusOptionGroup(String caption, Container dataSource) {
		super(caption, dataSource);
		alignment = HORIZONTAL;
	}

	public IkarusOptionGroup(String caption) {
		super(caption);
		alignment = HORIZONTAL;
	}

	public void setItems(String items) {
		StringTokenizer st = new StringTokenizer(items, ",");
		ArrayList<String> options = new ArrayList<String>();
		while (st.hasMoreTokens()) {
			String item = (String) st.nextToken();
			options.add(item);
		}
		final Container c = new IndexedContainer();
		if (options != null) {
			for (final Iterator<?> i = options.iterator(); i.hasNext();) {
				c.addItem(i.next());
			}
		}
		setContainerDataSource(c);
	}

	public void setAlignment(String alignment) {
		this.alignment = alignment;
	}

	public void paintContent(PaintTarget target) throws PaintException {
		super.paintContent(target);
		target.addAttribute("alignment", alignment);
	}

}
